<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Admin\Item_attribute;
use App\Admin\Item_stock;
use Input;

class Item_attributeController extends Controller
{
    public function add(){
        if(Input::method() == 'POST'){
            $data = Input::all();
            $result = Item_attribute::insert([
                'item_id'         =>	$data['item_id'],
                'attribute_id'    =>	$data['attribute_id'],
                'attribute_value' =>	$data['attribute_value'],
            ]);
            return $result ? '1' : '0';
        }else{

        }
    }

    public function del(){
        $id = Input::get('id');
        $item_id = Input::get('item_id');
        $stock = Item_stock::where('item_id',$item_id) -> get() -> toArray();
        $attr_ids = '';
        foreach($stock as $k => $v){
            $attr_ids .= $v['item_attribute_ids'] . ',';
        }
        unset($k,$v);
        $attr_ids = array_unique(explode(',',rtrim($attr_ids,',')));
        if(in_array($id,$attr_ids) == true ){
            return '0';
        }else{
            $result = Item_attribute::where('id',$id) -> delete();
            return $result ? '1' : '0';
        }
    }
}